import Vue from 'vue'
import Vuex from 'vuex'
import WebSocket from '../webSocket/webSocket'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    pageState:0,
    //定义页数
    size:1,
    infromation:{},
    //关键词
    word:'',
    
  },
  mutations: {
    //确定是什么页面
    changePage(state,page){
       state.pageState = page;
    },
    //传递页数
    changeSize(state,sizeNum){
      state.size = sizeNum;
    },
    //给state赋上消息对象
    getInfor(state,infromation){
       state.infromation  = infromation;
       console.log(state)
    },
    //拿到关键词
    sendWord(state,val){
         state.word = val;
    }
  },
  getters:{
    getKeyWord:state=>{
      return state.word
    }
  },
  actions: {
    //获取webSocket的message
    getSocket(context){
      WebSocket.ws.onmessage = ({ data })=> {
        if (data !== "连接成功" && data !== "消息接受成功") {
          data = JSON.parse(data);
          console.log(data)
          context.commit('getInfor',data);
          return data
        }
      };
    
    },
    getWord(context,val){
      
      context.commit('sendWord',val)
   }
  },

  modules: {
  }
})
